layui.use(['layer','treeSelect','formSelects','table','form','laytpl','element'], function(){
    var $ = layui.$
        ,layer = layui.layer
        ,admin = layui.admin
        ,table = layui.table
        ,element = layui.element
        ,form = layui.form
        ,laytpl = layui.laytpl;
    var delFlag = null;
    var treeSelect= layui.treeSelect;//树形下拉框
    var formSelects = layui.formSelects;

    //获取路径参数
    var request = GetRequest();
    $("#templateId").val(request.id);

    //树形下拉分类
    treeSelect.render({
        elem: '#tree-select',
        data: '/process/tree/select/' + request.id,
        type: 'get',
        placeholder: '请选择',// 占位符
        search: true,// 是否开启搜索功能：true/false，默认false
        click: function(data){
            $("#tree-select").val(data.current.id);
        },
        success: function (data) { // 加载完成后的回调函数
            $("#tree-select").val("0");
        }
    });

    //加载表格类型
    $.get("/dictionary/list/c5c15a72bcd340b1a8b65053acfa9111",function (data) {
        $.each(data.data,function (i,ele) {
            $("#tableType").append('<option value="'+ ele.value +'">'+ ele.name +'</option>');
        });
        $.ajax({
            url: '/table/list',
            type: 'get',
            async: false,
            success: function (table) {
                $.each(table.data,function (i,ele) {
                    $("#tableType").append('<option value="'+ ele.name +'">'+ ele.nameZh +'</option>');
                });
            }
        });
        formSelects.render();
    });

    //添加生产流程信息
    form.on('submit(add)', function(data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            url: '/process/add',
            data: data.field,
            type:'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("添加失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("添加失败！");
            }
        });
        return false;
    });

    /**
     * 获取路径中的参数
     * @returns {Object}
     */
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }


});

